<template>
	<view>
		<view class="cu-card article">
			<view class="cu-item shadow">
				<view class="flex solid-bottom padding justify-between">
					<view class="text-lg">
						<view style="background: #fff;">
							<view class="t-login">
								<u--form labelPosition="left" labelWidth="80" :model="userInfo" :rules="rules"
									ref="uForm">
									<u-form-item label="司机姓名" prop="driverName" ref="item1" :required="true">
										<u--input type="text" clearable v-model="userInfo.driverName" border="surround">
										</u--input>
									</u-form-item>
									<u-form-item label="手机号码" prop="phoneNum" ref="item1" :required="true">
										<u--input type="number" clearable v-model="userInfo.phoneNum" border="surround">
										</u--input>
									</u-form-item>
									<u-form-item v-if="fieldDisplay.idCard==1" label="身份证号" prop="idCard" ref="item1"
										:required="true">
										<u--input type="idCard" clearable v-model="userInfo.idCard" border="surround">
										</u--input>
									</u-form-item>
									<u-form-item v-if="fieldDisplay.driversLicense==1" prop="image" labelPosition="top"
										border-bottom label="驾驶证" :required="true">
										<u-upload accept="image" :fileList="fileList3" @afterRead="afterRead"
											@delete="deletePic" name="3" multiple :maxCount="1">
										</u-upload>
									</u-form-item>
									<u-button type="primary" :loading="addLoading" :disabled="addLoading" text="修改"
										@click="addInfo"></u-button>
								</u--form>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAction,
		postAction,
		putAction
	} from '@/api/api.js';
	import config from '@/api/config.js'

	export default {
		data() {
			return {
				userInfo: {
					driverName: '',
					phoneNum: '',
					idCard: '',
					openId: '',
				},
				fileList3: [],
				rules: {
					'driverName': {
						type: 'string',
						required: true,
						message: '请填写司机姓名',
						trigger: ['blur', 'change']
					},
					'phoneNum': [
						{
						type: 'string',
						required: true,
						message: '请填写手机号码',
						trigger: ['blur', 'change']
					}, {
						type: 'string',
						required: true,
						min: 11,
						max: 11,
						message: '请填写11手机号码',
						trigger: ['blur', 'change']
					}],
					'idCard': [
						{
							type: 'string',
							required: true,
							message: '请填写身份证号',
							trigger: ['blur', 'change']
						},
						{
							type: 'string',
							required: true,
							min: 18,
							max: 18,
							message: '请填写18位身份证号',
							trigger: ['blur', 'change']
						}
					],
				},
				fieldDisplay: {},
				addLoading: false,
				urls: {
					addUser: '/personnelInformation/personnelInformation/m/add',
					editUser: '/personnelInformation/personnelInformation/m/edit',
				},
			}
		},
		onLoad() {
			this.fieldDisplay = uni.getStorageSync("fieldDisplay")
			this.getUser()
		},
		methods: {
			addInfo() {
				this.$refs.uForm.validate().then(res => {
					if (this.fieldDisplay.driversLicense == 1) {
						if (this.fileList3.length === 0) {
							return uni.$u.toast("请上传驾驶证")
						}
						if (!this.fileList3[0].aurl) {
							return uni.$u.toast("请等待上传完毕")
						}
						this.userInfo.driversLicense = this.fileList3[0].aurl;
					}
					this.addLoading = true;
					if (this.userInfo.id) { //编辑
						putAction(this.urls.editUser, this.userInfo).then((res) => {
							console.log(res)
							if (res.success) {
								this.getUser()
							}
							uni.$u.toast(res.message)
						})
					} else { //新增
						let openId = uni.getStorageSync("openId")
						this.userInfo.openId = openId
						postAction(this.urls.addUser, this.userInfo).then((res) => {
							console.log(res)
							if (res.success) {
								this.getUser()
							}
							uni.$u.toast(res.message)
						})
					}
					this.addLoading = false
				}).catch((res) => {
					uni.showModal({
						title: '提示',
						content: res[0].message,
						showCancel: false,
						success: (res) => {
							if (res.confirm) {

							}
						}
					});
				})
			},
			getUser() {
				// let openId = uni.getStorageSync("openId")
				let openId = 'oFTXH5jAW_5SyyvcXGPTkuosS4bw'
				getAction('/carRegister/carRegister/m/getUserByOpenId', {
					openId: openId
				}).then(res => {
					if (res.success && res.result) {
						this.fileList3 = []
						this.userInfo = res.result
						this.fileList3.push({
							url: res.result.driversLicense,
							aurl: res.result.driversLicense
						})
					} else {
						this.userInfo.openId = openId
					}
				})
			},
			async afterRead(event) {// 新增图片
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					await this.uploadFilePromise(lists[i].url, event.name).then((result) => {
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							aurl: result.message
						}))
						fileListLen++
					})
				}
			},
			deletePic(event) {// 删除图片
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			uploadFilePromise(url, name) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl + config.imgUpload,
						filePath: url,
						name: 'file',
						formData: {
							name: name,
						},
						success: (res) => {
							let result = JSON.parse(res.data)
							if (result.success) {
								setTimeout(() => {
									resolve(result)
								}, 100)
							} else {
								reject(result)
							}
						}
					});
				})
			},
		}
	}
</script>

<style>
	.login-view {
		width: 100%;
		position: relative;
		background-color: #ffffff;
		border-radius: 8% 8% 0% 0;
	}
</style>